<template>
  <section class="firm-manage" v-cloak>
    <div class="info-title"><span class="icon"></span>接口列表</div>
    <form @keyup.enter="search" @submit.prevent>
      <el-row class="search-box">
        <el-col :span="24">
          <label for="" class="choose">
            <span>接口名称：</span>
            <el-select v-model="form.interface_id" placeholder="请选择接口名称"  class="width200">
              <el-option v-for="item in interfacebaseList" :key="item.id" :label="item.Name" :value="item.Id"></el-option>
            </el-select>
          </label>
          <label for="" class="choose">
            <span>地区：</span>
            <el-select v-model="form.province" placeholder="请选择省份" class="width200">
              <el-option v-for="item in provinceList" :key="item.id" :label="item.name" :value="item.name"></el-option>
            </el-select>
          </label>
          <label for="" class="choose">
            <el-button icon="search" type="primary" size="small" @click="search" v-resetClick>搜索</el-button>
          </label>
        </el-col>
      </el-row>
    </form>
    <el-row class="title">
      <el-button class="titleright" type="primary" size="small" @click="addFirm" v-resetClick>新增</el-button>
    </el-row>
    <el-row class="data-list">
      <ctable :isLoading="isLoading"  :thead="thead" :table-data="lists" :options="options" :page-data="pageObj" all-align="center"></ctable>
    </el-row>
    <el-dialog :title="ruleForm.id ? '编辑接口' : '新增接口'" :visible.sync="isDialogShow" :show-close="false" :destroy-on-close="true" :close-on-click-modal="false" width="40%" custom-class="customWidth">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="接口名称:" :label-width="formLabelWidth" prop="interface_id">
          <el-select v-model="ruleForm.interface_id" placeholder="请选择接口名称" style="width: 350px;">
            <el-option v-for="item in interfacebaseList" :key="item.id" :label="item.Name" :value="item.Id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="语音播报:" :label-width="formLabelWidth">
          <el-form-item label="红码:" prop="content.redCode" style="margin-bottom: 25px;">
            <el-input v-model="ruleForm.content.redCode" autocomplete="off" style="width: 300px;" placeholder="请输入红码播报内容"></el-input>
          </el-form-item>
          <el-form-item label="黄码:" prop="content.YellowCode" style="margin-bottom: 25px;">
            <el-input v-model="ruleForm.content.YellowCode" autocomplete="off" style="width: 300px;" placeholder="请输入黄码播报内容"></el-input>
          </el-form-item>
          <el-form-item label="绿码:" prop="content.greenCode">
            <el-input v-model="ruleForm.content.greenCode" autocomplete="off" style="width: 300px;" placeholder="请输入绿码播报内容"></el-input>
          </el-form-item>
        </el-form-item>
        <el-form-item label="地  区:" :label-width="formLabelWidth" prop="province">
          <el-select v-model="ruleForm.province" placeholder="请选择省份" style="width: 350px;">
            <el-option v-for="item in provinceList" :key="item.id" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备  注:" :label-width="formLabelWidth">
          <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 6 }" placeholder="请输入备注信息" v-model="ruleForm.remarks" autocomplete="off" style="width: 350px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm('ruleForm', 2)">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm', 1)">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { interfacelist, interfaceupdate, interfaceadd, interfacebase, interfacedel, interfaceinfo } from 'api/txEquipment/index'
import common from 'src/util/common'
import ctable from 'components/common/ctable'
export default {
  data () {
    return {
      isDialogShow: false,
      formLabelWidth: '120px',
      formLabelWidth2: '60px',
      ruleForm: {
        name: '',
        interface_id: '',
        content: {
          redCode: '',
          YellowCode: '',
          greenCode: ''
        },
        remarks: '',
        province: ''
      },
      form: {
        interface_id: '',
        province: ''
      },
      formObj: {},
      rules: {
        interface_id: [
          { required: true, message: '请选择接口名称', trigger: 'change' }
        ],
        'content.redCode': [
          { required: true, message: '请输入红码播报内容', trigger: 'blur' }
        ],
        'content.YellowCode': [
          { required: true, message: ' 请输入黄码播报内容', trigger: 'blur' }
        ],
        'content.greenCode': [
          { required: true, message: '请输入绿码播报内容', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '请选择省份', trigger: 'change' }
        ]
      },
      shopName: '', // 厂商名称
      lists: [], // 数据列表,
      thead: [{
        name: '接口名称',
        prop: 'name'
      }, {
        name: '创建时间',
        prop: 'created'
      }, {
        name: '地区',
        prop: 'province'
      }, {
        name: '备注',
        prop: 'remarks'
      }],
      options: [{
        name: '编辑',
        callback: this.editFirm
      }, {
        name: '删除',
        callback: this.delEvemt
      }],
      pageObj: { // 分页
        page: {},
        callback: this.getData
      },
      provinceList: common.province,
      interfacebaseList: []
    }
  },
  created () {
    this.filterObjEvemt()
    interfacebase().then(res => {
      this.interfacebaseList = res.data || []
    })
  },
  components: {
    ctable
  },
  methods: {
    submitForm (formName, num) {
      if (num === 1) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.ruleForm.id) {
              interfaceupdate(this.ruleForm).then(res => {
                this.$message({
                  message: '编辑成功',
                  type: 'success'
                })
                this.isDialogShow = false
                this.getData(1)
              })
            } else {
              interfaceadd(this.ruleForm).then(res => {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.isDialogShow = false
                this.getData(1)
              })
            }
          }
        })
      } else {
        this.$refs[formName].resetFields()
        this.isDialogShow = false
      }
    },
    getData (int) { // 获取数据
      interfacelist({
        pageSize: 10,
        pageIndex: int - 1,
        ...this.formObj
      }).then(res => {
        this.lists = res.data
        this.pageObj.page = res.page
      })
    },
    search () {
      this.filterObjEvemt()
    },
    // 新增厂商
    addFirm () {
      this.ruleForm = {
        // name: '',
        interface_id: '',
        content: {
          redCode: '',
          YellowCode: '',
          greenCode: ''
        },
        remarks: '',
        province: ''
      }
      this.isDialogShow = true
    },
    // 编辑厂商
    editFirm (data) {
      interfaceinfo({id: data.id}).then(res => {
        this.ruleForm = {
          id: res.data.id,
          // name: res.data.name,
          interface_id: res.data.interface_id,
          content: res.data.content,
          remarks: res.data.remarks,
          province: res.data.province
        }
        this.isDialogShow = true
      })
    },
    // 删除
    delEvemt (data) {
      this.$confirm('是否删除该接口？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        interfacedel({id: data.id}).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getData(1)
        })
      }).catch(() => {
      })
    },
    filterObjEvemt () {
      let obj = {}
      for (const key in this.form) {
        if (this.form[key] === '' || this.form[key] === null || this.form[key] === undefined) continue
        switch (key) {
          case 'remarks8888':
            obj[key] = this.form[key]
            break
          default:
            obj[key] = this.form[key]
            break
        }
      }
      this.formObj = JSON.parse(JSON.stringify(obj))
      this.getData(1)
      return obj
    }
  }
}
</script>
<style lang="less">
  .itemBoxName .el-form-item__error{
    z-index: 100;
    top: 60%;
  }
</style>
<style lang="less" scoped>
.firm-manage {
  /deep/ .customWidth{
    min-width: 560px;
  }
  .width200{width: 200px;}
  .title {
    overflow: hidden;
    padding: 16px 30px;
    padding-bottom: 0;
  }
  .search-box {
    padding: 20px 30px;
    background: #ffffff;
  }
  .data-list {
    padding: 10px 30px;
  }
  .choose {
    margin-right: 10px;
    .select-box {
      width: 120px;
    }
    .year-box {
      width: 150px;
    }
  }
  .titleright{
    float: right;
    margin-left: 20px;
  }
}
</style>
